﻿/*
  Copyright © Alexander G. Bykin, Russia 2010
  This source is subject to the Microsoft Public License (Ms-PL).
  Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
  Project link: http://datagridriaex4sl.codeplex.com/
  Related project link: http://riadatafilter.codeplex.com/
  All other rights reserved.
*/

namespace DataGridRIAExtension
{
    using DG = System.Windows.Controls.DataGrid;
    using System.ComponentModel;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;

    public static class DataGrid
    {
        #region DependencyProperty ParseHeadersMode

        public static DependencyProperty ParseHeadersModeProperty =
            DependencyProperty.RegisterAttached(
                "ParseHeadersMode",
                typeof(bool),
                typeof(DataGrid),
                new PropertyMetadata(false, DataGrid.ParseHeadersModePropertyChanged));

        public static bool GetParseHeadersMode(DependencyObject target)
        {
            return (bool)target.GetValue(DataGrid.ParseHeadersModeProperty);
        }

        public static void SetParseHeadersMode(DependencyObject target, bool mode)
        {
            target.SetValue(DataGrid.ParseHeadersModeProperty, mode);
        }

        private static void ParseHeadersModePropertyChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            DG dataGrid = sender as DG;

            if (dataGrid != null)
            {
                Shim.Create(dataGrid, (bool)e.NewValue);
            }
        }
        #endregion

        public class Shim
        {
            #region Private Properties

            #region Private Property IsFieldsHeaderGenerated

            private bool IsFieldsHeaderGenerated = false;

            #endregion

            #endregion

            #region Constructors

            public static Shim Create(DG dataGrid, bool mode)
            {
                Shim shim = new Shim();
                shim.Initialize(dataGrid, mode);
                return shim;
            }

            private void Initialize(DG dataGrid, bool useEagerSelection)
            {
                if (dataGrid != null && !this.FixColumns(dataGrid))
                {
                    dataGrid.Loaded += OnDataGridLoaded;
                }
            }

            #endregion

            #region Private Methods

            #region Private Method OnDataGridLoaded

            private void OnDataGridLoaded(object sender, RoutedEventArgs e)
            {
                this.FixColumns(sender);
            }

            #endregion

            #region Private Method FixColumns

            private bool FixColumns(object sender)
            {
                DG dataGrid = sender as DG;

                if (this.IsFieldsHeaderGenerated || !GetParseHeadersMode(dataGrid))
                {
                    return this.IsFieldsHeaderGenerated;
                }

                BindingExpression be = dataGrid.GetBindingExpression(System.Windows.Controls.DataGrid.ItemsSourceProperty);
                if (be != null)
                {
                    if (be.DataItem != null && be.DataItem.GetType() == typeof(DomainDataSource))
                    {
                        var domainFields = Utilities.GetListOfDomainEntityFields((DomainDataSource)be.DataItem);
                        foreach (var column in dataGrid.Columns.Where(r => r.GetType() == typeof(DataGridTextColumn) || r.GetType() == typeof(DataGridCheckBoxColumn)))
                        {
                            DomainEntityField domainField = null;

                            if (column.GetType() == typeof(DataGridTextColumn))
                            {
                                domainField = domainFields.Where(r => r.PropertyPath == ((DataGridTextColumn)column).Binding.Path.Path).FirstOrDefault();
                            }

                            if (column.GetType() == typeof(DataGridCheckBoxColumn))
                            {
                                domainField = domainFields.Where(r => r.PropertyPath == ((DataGridCheckBoxColumn)column).Binding.Path.Path).FirstOrDefault();
                            }

                            if (domainField != null)
                            {
                                column.Header = domainField.DisplayName;
                            }
                        }
                        this.IsFieldsHeaderGenerated = true;
                    }
                    else
                    {
                        System.Diagnostics.Debug.WriteLine("BindingExpression.DataItem = null, exiting.");
                    }
                }

                return this.IsFieldsHeaderGenerated;
            }

            #endregion

            #endregion
        }
    }
}
